package com.example.aigc_education.service.converter;

import com.example.aigc_education.domain.Plan;
import com.example.aigc_education.domain.TodoV2;
import com.example.aigc_education.domain.request.AddTodoRequest;
import com.example.aigc_education.domain.request.UpdateTodoRequest;
import com.example.aigc_education.domain.vo.ListPlanVO;
import com.example.aigc_education.domain.vo.TodoVO;
import com.example.aigc_education.domain.vo.UpdateTodoVO;
import com.example.aigc_education.enums.*;
import org.mapstruct.*;

import java.util.List;
@Mapper(componentModel = "spring")
public interface TodoConverter {

    /**
     * AddTodoRequest-->TodoV2
     */
    @Mappings(
            value = {
                    @Mapping(source = "title", target = "title"),
                    @Mapping(target = "priority", ignore = true),
                    @Mapping(source = "deadline", target = "deadline"),
                    @Mapping(target = "todoType",ignore = true),
                    @Mapping(target = "focusMode", ignore = true),
                    @Mapping( target = "timeMode",ignore = true)
            }
    )
    TodoV2 addTodoRequestToTodoV2(AddTodoRequest addTodoRequest);

    @AfterMapping
    default void afterMapping(AddTodoRequest addTodoRequest, @MappingTarget TodoV2 todoV2){
        todoV2.setPriority(TodoPriorityType.fromTitle(addTodoRequest.getPriority()).getValue());
        todoV2.setTodoType(TodoType.fromTitle(addTodoRequest.getTodoType()).getValue());
        todoV2.setFocusMode(addTodoRequest.getFocusMode()?1:0);
        if (!addTodoRequest.getFocusMode()){
            todoV2.setTimeMode(TodoTimerModeType.NO_FOCUS.getValue());
            return;
        }
        todoV2.setTimeMode(TodoTimerModeType.fromTitle(addTodoRequest.getTimerMode()).getValue());
        if (addTodoRequest.getTimerMode().equals("normal")){
            todoV2.setFocusTime(addTodoRequest.getTargetTime());
        }else {
            todoV2.setFocusTime(addTodoRequest.getPomodoroLength());
        }
    }

    List<TodoV2> addTodoRequestsToTodoV2s(List<AddTodoRequest> addTodoRequests);


    /**
     * updateTodoRequest-->TodoV2
     */
    @Mappings(
            value = {
                    @Mapping(target = "priority", ignore = true),
                    @Mapping(target = "todoType",ignore = true),
                    @Mapping(target = "focusMode", ignore = true),
                    @Mapping( target = "timeMode",ignore = true)
            }
    )
    TodoV2 updateTodoRequestToTodoV2(UpdateTodoRequest updateTodoRequest);
    @AfterMapping
    default void afterMapping(UpdateTodoRequest updateTodoRequest, @MappingTarget TodoV2 todoV2){
        todoV2.setPriority(TodoPriorityType.fromTitle(updateTodoRequest.getPriority()).getValue());
        todoV2.setTodoType(TodoType.fromTitle(updateTodoRequest.getTodoType()).getValue());
        todoV2.setFocusMode(updateTodoRequest.getFocusMode()?1:0);
        if (!updateTodoRequest.getFocusMode()){
            todoV2.setTimeMode(TodoTimerModeType.NO_FOCUS.getValue());
            return;
        }
        todoV2.setTimeMode(TodoTimerModeType.fromTitle(updateTodoRequest.getTimerMode()).getValue());
        if (updateTodoRequest.getTimerMode().equals("normal")){
            todoV2.setFocusTime(updateTodoRequest.getTargetTime());
        }else {
            todoV2.setFocusTime(updateTodoRequest.getPomodoroLength());
        }
    }

    /**
     * updateTodoRequest-->UpdateTodoVO
     */
    UpdateTodoVO updateTodoRequestToUpdateTodoVO(UpdateTodoRequest updateTodoRequest);

    /**
     * TodoV2-->TodoVO
     */
    @Mappings(
            value = {
                    @Mapping(source = "todoId", target = "id"),
                    @Mapping(source = "title", target = "title"),
                    @Mapping(target = "priority", ignore = true),
                    @Mapping(source = "deadline", target = "deadline"),
                    @Mapping(target = "todoType",ignore = true),
                    @Mapping(target = "focusMode", ignore = true),
                    @Mapping( target = "timerMode",ignore = true),
                    @Mapping( target = "completed",ignore = true)
            }
    )
    TodoVO todoV2ToTodoVO(TodoV2 todoV2);

    @AfterMapping
    default void afterMapping(TodoV2 todoV2, @MappingTarget TodoVO todoVO){
        todoVO.setPriority(TodoPriorityType.fromValue(todoV2.getPriority()).getTitle());
        todoVO.setTodoType(TodoType.fromValue(todoV2.getTodoType()).getTitle());
        todoVO.setFocusMode(todoV2.getFocusMode()==1);
        todoVO.setCompleted(TodoCompletedType.fromValue(todoV2.getCompleted()).getTitle());
        if (todoV2.getFocusMode() == 0){
            todoVO.setTimerMode(TodoTimerModeType.NO_FOCUS.getTitle());
           return;
        }
        todoVO.setTimerMode(TodoTimerModeType.fromValue(todoV2.getTimeMode()).getTitle());
        if (todoV2.getTimeMode() == TodoTimerModeType.NORMAL.getValue()){
            todoVO.setTargetTime(todoV2.getFocusTime());
        }else {
            todoVO.setPomodoroLength(todoV2.getFocusTime());
        }
    }
    List<TodoVO> todoV2sToTodoVOs(List<TodoV2> todoV2s);

}
